﻿@{
    // Set the layout page and page title
    LayoutPage = "~/_SiteLayout.cshtml";
    PageData["Title"] = "Log In";
    
    // Initialize general page variables
    var username = "";
    var password = "";
    var rememberMe = false;

    // Validation
    var isValid = true;
    var usernameErrorMessage = "";
    var passwordErrorMessage = "";

    // If this is a POST request, validate and process data
    if (IsPost) {
        username = Request.Form["username"];
        password = Request.Form["password"];
        rememberMe = Request.Form["rememberMe"].AsBool();
        
        // Validate the user's username
        if (username.IsEmpty()) {
            usernameErrorMessage = "You must specify a username.";
            isValid = false;
        }
        
        // Validate the user's password
        if (password.IsEmpty()) {
            passwordErrorMessage = "You must specify a password.";
            isValid = false;
        }
        
        // Confirm there are no validation errors
        if (isValid) {
            // Attempt to login to the Security object using provided creds
            if (WebSecurity.Login(username, password, rememberMe)) {
                Response.Redirect("~/");
            }
            
            // If we arrived here, the login failed; convey that to the user
            isValid = false;
        }    
    }
}

<p>
   Please enter your username and password below. If you don't have an account,
   visit the <a href="@Href("~/Account/Register")">registration page</a> and create one.
</p>

@// If one or more validation errors exist, show an error 
@if (!isValid) {
   <div class="message error icon">There was a problem with your login and/or errors exist in your form.</div>
}

<fieldset class="prettyForm">
    <legend>Log In to Your Account</legend>
    <form method="post" action="">
        <div>
            <label for="username" id="labelUsername">Username:</label>
            <input type="text" id="username" name="username" value="@username" />
            @// Write any username validation errors to the page 
            @if (!usernameErrorMessage.IsEmpty()) {
                <span class="message error">&raquo; @usernameErrorMessage</span>
            }
        </div>         
        <div>
            <label for="password" id="labelPassword">Password:</label>
            <input type="password" id="password" name="password" value="" />
            @// Write any password validation errors to the page 
            @if (!passwordErrorMessage.IsEmpty()) {
                <span class="message error">&raquo; @passwordErrorMessage</span>
            }
        </div>
        <div>
            <label class="inline-checkbox" for="rememberMe" id="labelRememberMe">Remember Me?</label>
            <input type="checkbox" id="rememberMe" name="rememberMe" value="true"@(rememberMe ? @" checked=""checked""" : "") />
        </div>
        <div>
            <input type="submit" value="Login" />
        </div>
        <div>
            <a href="ForgotPassword">Did you forget your password?</a>
        </div>
    </form>
</fieldset>